package com.songlin.houxin.admin.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlin.houxin.admin.api.dto.RegionDTO;
import com.songlin.houxin.admin.api.entity.Region;
import com.songlin.houxin.admin.api.vo.RegionVO;
import com.songlin.houxin.admin.mapper.RegionMapper;
import com.songlin.houxin.admin.service.RegionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @author jinYuLinLang
 * @version 1.0.0
 * @ClassName RegionServiceImpl.java
 * @Description <p></p>
 * @createTime 2022-03-12 06:48:00
 */
@Service
@RequiredArgsConstructor
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService {


    private static final Map<String, List<RegionVO>> regionMap = new ConcurrentHashMap<>();
    private static String REGION = "region";

    @Override
    public Collection<RegionVO> queryRegionList(RegionDTO dto) {
        StringBuilder sb = new StringBuilder(REGION);
        Integer type = dto.getType() == null ? 1 : dto.getType();
        Optional.ofNullable(dto.getType()).ifPresent(sb::append);
        Optional.ofNullable(dto.getParentId()).ifPresent(sb::append);
        List<RegionVO> vos = regionMap.get(sb.toString());
        if (CollectionUtil.isNotEmpty(vos)) {
            return vos;
        }
        LambdaQueryWrapper<Region> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(Region::getType, type);
        List<Region> regionList = this.list(wrapper);
        regionList.sort(Comparator.comparingInt(Region::getId));
        List<Region> oneList = regionList.stream().filter(region -> region.getType().equals(type)).collect(Collectors.toList());
        List<RegionVO> regionVOList = new ArrayList<>();
        for (Region region : oneList) {
            //RegionVO regionVO = RegionConverter.INSTANCE.toRegionVO(region);
            RegionVO regionVO = JSONUtil.toBean(JSONUtil.toJsonStr(region), RegionVO.class);
            regionVOList.add(regionVO);
            buildRegion(regionVO, regionList);
        }
        regionMap.put(sb.toString(), regionVOList);
        return regionVOList;
    }

    private void buildRegion(RegionVO vo, List<Region> regionList) {
        List<Region> regions = regionList.stream().filter(region -> vo.getId().equals(region.getParentId())).collect(Collectors.toList());
        List<RegionVO> vos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(regions)) {
            for (Region region : regions) {
                RegionVO regionVO = JSONUtil.toBean(JSONUtil.toJsonStr(region), RegionVO.class);
                vos.add(regionVO);
                buildRegion(regionVO, regionList);
            }
            vo.setSubRegionList(vos);
        }
    }
}
